
var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,laydate = layui.laydate;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});
function getAllJson(jsons) {
    let tmpitem=[]
    for (let item of jsons) {
        let tmparr={}
        tmparr["value"]=item["code"];
        tmparr["name"]=item["name"];
        if(item["users"]){
            for(let it of item["users"]){
                item["list"].push(it)
            }
        }
        if(item["list"] && item["list"].length){
            tmparr["children"]=   this.getAllJson(item["list"]);
            tmpitem.push(tmparr)
        }else{
            tmpitem.push(tmparr)
        }

    }
    return tmpitem
}
function getxmselect(el,name, data) {
    return xmSelect.render({
        el: el,
        name: name,
        tree: {
            show: true,
            showLine: true,
            showFolderIcon:true,
            indent: 20,
            expandedKeys:false
        },
        height: 'auto',
        data: data,
        on: function ({ arr, change, isAdd }) {
            return this
        }
    })
}

//select over
(function () {
    let salesmanList = getCurrentDepartmentUser();
    if(salesmanList && salesmanList.list){
        let salesmanTree = getAllJson(salesmanList.list);
        getxmselect("#salesmanCode","salesmanCode", salesmanTree)
    }else {
        getxmselect("#salesmanCode","salesmanCode", [])
    }
})()

let loginUserCode;
layer.ready(function () {
    // $.ajax({
    //     url:"cost/getLoginUserInfo",
    //     async:false,
    //     success:function(data){
    //         if(data.status == 200){
    //             loginUserCode=data.data.code;
    //             //当前登录人所管理的员工:业务员
    //             let manageSalesmanRulCost=getManageRule("costSalesman");
    //             for(let i in manageSalesmanRulCost){
    //                 $("#salesmanCode").append("<option value=\"" + manageSalesmanRulCost[i].code + "\">" + manageSalesmanRulCost[i].name + "</option>");
    //             }
    //             form.render('select');
    //         }
    //     }
    // });
});
table.render({
    elem: '#mains'
    ,url:'cost/getInvoiceListByPage'
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'invoiceCode', title:'发票编号',sort:true}
        ,{field: 'stockNoStr', width: 120, title: '采购合同号', sort: true}
        ,{field:'contractNo', title:'外销合同号',sort:true}
        ,{field:'customerCode', hide:true}
        ,{field:'customerName', title:'客户名称',sort:true}
        ,{field:'customerContractNo', title:'客户合同号',sort:true}
        ,{field:'salesmanName', title:'业务员',sort:true}
        ,{field:'salesmanCode', hide:true}
        ,{field:'operatorName', title:'操作员',sort:true}
        ,{field:'operatorCode', hide:true}
    ]]
    ,id: 'mainstableid'
    ,height: 500
    ,page: true
});

form.on('submit(submitform)', function(data){
    table.reload('mainstableid', {
        url:'cost/getInvoiceListByPage',
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    var purchaseorderdata =   table.checkStatus("mainstableid").data;
    if(purchaseorderdata.length == 0){
        layer.msg("请选择数据")
    }else{
        var oldData =  parent.table.cache["maininformationtableid"];
        var newData=[];
        var num=oldData.length+1;
        for (let i in purchaseorderdata){
            //purchaseorderdata[i].tempId=num;
            purchaseorderdata[i].tempId=Number(Math.random().toString().substr(3,3) + Date.now()).toString(36);
            purchaseorderdata[i]['billDate']=purchaseorderdata[i].invoiceDateStr;
            purchaseorderdata[i]['increaseCode'] = "无";
            newData.push(purchaseorderdata[i]);
            num++;
        }
        oldData.push.apply(oldData, newData);
        //重载产品明细表单
        parent.table.reload('maininformationtableid', {data:oldData, limit:oldData.length});//刷新主信息表格


        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },1000)
    }
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})